<?php
	if(!isset($_REQUEST['ajax'])){ $_REQUEST['ajax'] = "";}
	if($_REQUEST['ajax']!=true) include 'header.tpl.php';
?>
	<div class="row">
		<div class="col-lg-12">
			<ul class="shortcut-buttons-set">
				<li>
					<a class="shortcut-button" href='#request_list'><span>
					<i class="aicon-task_list"></i>
					<?php echo $this->language['list'];?></span></a>
				</li>
				<li>
					<a class="shortcut-button" href='#request_list/status=0'><span>
					<i class="aicon-clock"></i>
					<?php echo $this->language['waiting'];?></span></a>
				</li>
				<li>
					<a class="shortcut-button" href='#request_create' ><span>
					<i class="aicon-plus"></i>
					<?php echo $this->language['request_create'];?></span></a>
				</li>
				<?php if(check_permission('request_view')): ?>
				<li>
					<a class="shortcut-button" href='#request_report'><span>
					<i class="aicon-chart"></i>
					Xem báo cáo</span></a>
				</li>
				<?php endif;?>
			</ul>
		</div>
	</div>
	<input type="hidden" id="reqid" value="<?php echo $this->phieu['req_id'];?>">
	<div class="row">
		<div class="col-lg-12">
			<div class="panel">
				<div class="panel-heading">
					<div class="panel-title"><i class="aicon-task_content"></i> <?php echo $this->language['requests'];?></div>
				</div>
				<div class="panel-body">
					<div id="printable">
						<div class="receipt" >
							<div class="row">
								<div class="col-md-9 col-xs-9 text-center">
									<b>Công ty Cổ phần Văn hóa Tân Bình</b><br>
									Lô II-3, Nhóm CN2, Đường số 11, KCN Tân Bình, P.Tây Thạnh, Q.Tân Phú, Tp.HCM
								</div>
								<div class="col-md-3 col-xs-3 text-center">
									Số phiếu : <font color=red><?php echo $this->phieu['req_code'];?></font>
								</div>
							</div>
							<div class="row">
								<div class="col-md-12 col-xs-12 text-center">
									<h3>GIẤY ĐỀ NGHỊ TẠM ỨNG</h3>
								</div>
							</div>
							<div class="row">
								<div class="col-md-offset-1 col-xs-offset-1 col-md-3 col-xs-3">
									<b>Người đứng tên :</b>
								</div>
								<div class="col-md-8 col-xs-8">
									<font color=red><?php echo $_SESSION['user_data'][$this->phieu['user_id']]['fullname'];?></font>
								</div>
							</div>
							<div class="row">
								<div class="col-md-offset-1 col-xs-offset-1 col-md-3 col-xs-3">
									<b>Phòng ban :</b>
								</div>
								<div class="col-md-8 col-xs-8">
									<font color=red><?php echo $_SESSION['user_data'][$this->phieu['user_id']]['room_name'];?></font>
								</div>
							</div>
							<div class="row">
								<div class="col-md-offset-1 col-xs-offset-1 col-md-3 col-xs-3">
									<b>Công việc :</b>
								</div>
								<div class="col-md-8 col-xs-8">
									<?php
										if($this->phieu['task_code']!='') echo "<a href='#task_view/tid=".$this->phieu['tId']."'>".$this->phieu['tTitle']." ( ".$this->phieu['tCode']." )</a>";
										else echo "<font color=red>Không có</font>";

										if(check_permission('request_edit'))
											echo ' <a data-toggle="modal" href="#attach_task"><i class="aicon-task_name"></i></a>';
									?>
								</div>
							</div>
							<div class="row">
								<div class="col-md-offset-1 col-xs-offset-1 col-md-3 col-xs-3">
									<b>Số tiền :</b>
								</div>
								<div class="col-md-8 col-xs-8">
									<font color=red><?php echo number_format($this->phieu['amount'],0,"",",");?> VNĐ</font>
								</div>
							</div>
							<div class="row">
								<div class="col-md-offset-1 col-xs-offset-1 col-md-3 col-xs-3">
									<b>Bằng chữ :</b>
								</div>
								<div class="col-md-8 col-xs-8">
									<font color=red><?php echo $this->phieu['bangchu'];?></font>
								</div>
							</div>
							<div class="row">
								<div class="col-md-offset-1 col-xs-offset-1 col-md-3 col-xs-3">
									<b>Lý do :</b>
								</div>
								<div class="col-md-8 col-xs-8">
									<font color=red><?php echo $this->phieu['req_reason'];?></font>
								</div>
							</div>
							<div class="row">
								<div class="col-md-offset-1 col-xs-offset-1 col-md-3 col-xs-3">
									<b>Hạn thanh toán :</b>
								</div>
								<div class="col-md-8 col-xs-8">
									<font color=red><?php echo date("d/m/Y",$this->phieu['req_deadline']);?></font>
								</div>
							</div>
							<?php if($this->phieu['req_files'] != '' && is_array($this->phieu['req_files'])):?>
							<div class="row">
								<div class="col-md-offset-1 col-xs-offset-1 col-md-3 col-xs-3">
									<b>File chứng từ :</b>
								</div>
								<div class="col-md-8 col-xs-8">
									<ul>
										<?php foreach($this->phieu['req_files'] as $file):?>
										<li><a href='download.php?file=<?php echo $file;?>'><?php echo $file;?></a></li>
										<?php endforeach;?>
									</ul>
								</div>
							</div>
							<?php endif;?>
							<div class="row">
								<div class="col-md-offset-1 col-xs-offset-1 col-md-3 col-xs-3">
									<b>Tình trạng :</b>
								</div>
								<div class="col-md-8 col-xs-3">
									<?php
									if($this->phieu['req_signature1']=='0') echo "<font color=red>Chờ Duyệt</font>";
									elseif($this->phieu['req_status']=='0') echo "<font color=green>Đã Duyệt</font> - <font color=red>Chưa Chi</font>";
									elseif($this->phieu['req_status']=='1') echo "<font color=green>Đã Duyệt</font> - <font color=green>Đã Chi</font>";
									elseif($this->phieu['req_status']=='2') echo "<font color=blue>Đã Hoàn Ứng</font>";
									else echo "<font color=purple>Không biết</font>";
									?>
								</div>
							</div>
							<div class="row">
								<div class="col-md-offset-8 col-xs-offset-8 col-md-4 col-xs-4">
									Ngày <font color=red><?php echo date("d",$this->phieu['req_date']);?></font> Tháng <font color=red><?php echo date("m",$this->phieu['req_date']);?></font> Năm <font color=red><?php echo date("Y",$this->phieu['req_date']);?></font>
								</div>
							</div>
							<div class="row">
								<div class="col-md-12 col-xs-12">
									<table class="table text-center">
										<tr>
											<td><b>Giám đốc</b></td>
											<?php if($this->phieu['line_user_id']>0){ ?><td><b>Người ủy nhiệm</b></td><?php }?>
											<td><b>Kế toán trưởng</b></td>
											<td><b>Trưởng bộ phận</b></td>
											<td><b>Người xin tạm ứng</b></td>
										</tr>
										<tr>
											<td><?php if($this->phieu['req_signature1']=='1') echo "<font color=green>Đã duyệt</font>";
												else echo "<font color=red>Chưa duyệt</font>";?></td>
											<?php if($this->phieu['line_user_id']>0){ ?><td><?php if($this->phieu['req_signature5']=='1') echo "<font color=green>Đã duyệt</font>";
												else echo "<font color=red>Chưa duyệt</font>";?></td><?php }?>
											<td><?php if($this->phieu['req_signature2']=='1') echo "<font color=green>Đã duyệt</font>";
												else echo "<font color=red>Chưa duyệt</font>";?></td>
											<td><?php if($this->phieu['req_signature3']=='1') echo "<font color=green>Đã duyệt</font>";
												else echo "<font color=red>Chưa duyệt</font>";?></td>
											<td><?php if($this->phieu['req_signature4']=='1') echo "<font color=green>Đã duyệt</font>";
												else echo "<font color=red>Chưa duyệt</font>";?></td>
										</tr>
										<tr>
											<td><?php if(isset($this->btnCEO)) echo $this->btnCEO;?></td>
											<?php if($this->phieu['line_user_id']>0){ ?><td><?php if(isset($this->btnUN)) echo $this->btnUN;?></td><?php }?>
											<td><?php if(isset($this->btnKT)) echo $this->btnKT;?></td>
											<td><?php if(isset($this->btnTP)) echo $this->btnTP;?></td>
											<td>&nbsp;</td>
										</tr>
									</table>
								</div>
							</div>
						</div>
						<div class="notPrint">
							<h2 class="text-center"><?php echo $this->language['request_detail'];?></h2>
							<table class="table receipt table-bordered">
								<thead>
									<tr>
										<th width='20px'>#</th>
										<th>Nội dung / mô tả</th>
										<th width='70px'>Đơn vị</th>
										<th width='80px'>Số lượng</th>
										<th width='80px'>Đơn giá</th>
										<th width='90px'>Thành tiền</th>
									</tr>
								</thead>
								<tbody>
									<?php if(isset($this->listdt)){ $STT = 0;foreach($this->listdt as $row){ $STT++;?>
									<tr>
										<td class="text-center"><?php echo $STT;?></td>
										<td>
											<p><?php echo $row['item_name'];?></p>
											<?php if($row['item_description']):?>
											<hr/><p><?php echo nl2br($row['item_description']);?></p>
											<?php endif;?>
										</td>
										<td><?php echo $row['item_unit'];?></td>
										<td class="text-center"><font color=red><?php echo $row['detail_amount'];?></font></td>
										<td class="text-center"><font color=red><?php echo number_format($row['item_price'],0,"",",");?></font></td>
										<td class="text-center"><font color=green><?php echo number_format($row['amount'],0,"",",");?></font></td>
									</tr>
									<?php }} ?>
								</tbody>
							</table>
						</div>
						<?php if(isset($this->return) && count($this->return) > 0):?>
						<div>
							<h2 class="text-center">PHIẾU HOÀN ỨNG</h2>
							<table class="table receipt table-bordered">
								<thead>
									<tr>
										<th width="20px">#</th>
										<th>Tên dịch vụ</th>
										<th width="90px">Đơn giá</th>
										<th width="90px">Số lượng</th>
										<th width="100px">Thành tiền</th>
										<th>Ghi chú</th>
									</tr>
								</thead>
								<tbody>
									<?php $stt=0;foreach($this->return['detail'] as $row):$stt++;?>
									<tr>
										<td><?php echo $stt;?></td>
										<td><?php echo $row['item_name'];?></td>
										<td><?php echo number_format($row['item_price'],0,'.',',');?></td>
										<td><?php echo number_format($row['detail_amount'],0,'.',',');?></td>
										<td><?php echo number_format($row['amount'],0,'.',',');?></td>
										<td><?php echo $row['item_description'];?></td>
									</tr>
									<?php endforeach;?>
								</tbody>
								<tfoot>
									<tr>
										<td colspan=2 class="text-right"><b>Tổng thành tiền</b></td>
										<td colspan=4><?php echo number_format($this->return['amount'],0,".",",");?> VNĐ</td>
									</tr>
									<tr>
										<td colspan=2 class="text-right"><b>Bằng chữ</b></td>
										<td colspan=4><?php echo ucfirst(trim(currency2text($this->return['amount'])));?></td>
									</tr>
									<?php if($this->phieu['amount']>$this->return['amount']){ $text = "Thu lại";$money = $this->phieu['amount']-$this->return['amount'];}
										else{ $text = "Chi thêm";$money = $this->return['amount']-$this->phieu['amount'];}
										?>
									<tr>
										<td colspan=2 class="text-right"><b><?php echo $text;?></b></td>
										<td colspan=4><?php echo number_format($money,0,".",",");?> VNĐ</td>
									</tr>
									<tr>
										<td colspan=2 class="text-right"><b>Tình trạng</b></td>
										<td colspan=4>
											<?php if($this->return['return_status'] == 1):?><font color="green">Đã duyệt hoàn ứng</font>
											<?php else:?><font color="red">Chưa duyệt</font><?php endif;?>
										</td>
									</tr>
								</tfoot>
							</table>
						</div>

						<?php endif;?>
					</div>
					<div class="text-center">
						<?php if(isset($this->btnVerReturn)){ echo $this->btnVerReturn;}?>
						<?php if(isset($this->btnVer)) echo $this->btnVer;?>
						<a href="javascript:;" title="In Phiếu" onClick="doPrint();" class="btn btn-warning">In phiếu</a>
						<?php if($this->phieu['return_id'] == 0 && $this->phieu['user_id'] == $_SESSION['userId']):?>
						<a href="#return_create/request=<?php echo $this->phieu['req_id'];?>" title="Tạo Hoàn Ứng" class="btn btn-danger">Tạo Hoàn Ứng</a>
						<?php endif;?>
						<?php if(check_permission('special_attach_ptu')): ?>
						<a href="javascript:;" data-toggle="modal" data-target="#update_file" class="btn btn-primary">Cập nhật Chứng từ</a>
						<?php endif;?>
					</div>
					<?php if(count($this->ptc)>0){ $total = 0;?>
					<h2 class="text-center"><?php echo $this->language['request_payment_attached'];?></h2>
					<table class="table receipt table-bordered">
						<thead>
							<tr>
								<th width='80px'>Mã phiếu</th>
								<th width='120px'>Người đứng tên</th>
								<th>Lý do</th>
								<th width='90px'>Số tiền</th>
							</tr>
						</thead>
						<tbody>
							<?php foreach($this->ptc as $row){
							if($row['pay_type']=='1') $total = $total - $row['pay_amount'];
							else $total = $total + $row['pay_amount'];?>
							<tr>
								<td><?php echo "<a href='#finance_view/id=".$row['pay_id']."'>".$row['pay_code']."</a>";?></td>
								<td><?php echo $row['pay_name'];?></td>
								<td><?php echo $row['pay_description'];?></td>
								<td><?php if($row['pay_type']=='1') echo "<font color='green'>+ ".number_format($row['pay_amount'],0,".",",")."</font>";
										else echo "<font color='red'>- ".number_format($row['pay_amount'],0,".",",")."</font>";?></td>
							</tr>
							<?php } ?>
						</tbody>
						<tfoot>
							<tr>
								<td><b>Tổng tiền</b></td>
								<td colspan=3><?php echo number_format($total,0,".",",");?> VNĐ</td>
							</tr>
							<tr>
								<td><b>Bằng chữ</b></td>
								<td colspan=3><?php echo ucfirst(trim(currency2text($total)));?></td>
							</tr>
							<?php if($this->phieu['amount']>$total){ $text = "Thu lại";$money = $this->phieu['amount']-$total;}
								else{ $text = "Chi thêm";$money = $total-$this->phieu['amount'];}
								?>
							<tr>
								<td><b><?php echo $text;?></b></td>
								<td colspan=3><?php echo number_format($money,0,".",",");?></td>
							</tr>
						</tfoot>
					</table>
					<?php } ?>
				</div>
			</div>
			<?php if(check_permission('special_attach_ptu')){ ?>
			<div class="panel">
				<div class="panel-heading">
					<div class="panel-title"><i class="icon-cog"></i> <?php echo $this->language['request_payment_attach'];?></div>
				</div>
				<div class="panel-body">
					<form action="javascript:doSearchPayPTU();" class="form-inline">
						<label>Tìm kiếm : </label>
						<input type="text" class="form-control" autocomplete="off" placeholder="Mã phiếu chi" id="search_code">
						<input type="text" class="form-control" autocomplete="off" placeholder="Tên / Lý do" id="search_tieuchi">
						<input type="submit" value="Tìm" class="btn btn-warning" >
					</form>
					<div id="pay_result"></div>
				</div>
			</div>
			<?php } ?>
		</div>
	</div>

	<div class="modal fade" id="update_file">
		<div class="modal-dialog">
			<div class="modal-content">
				<form action="javascript:upload_req_file('<?php echo $this->phieu['req_code'];?>');">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						<h4 class="modal-title">Cập nhật chứng từ</h4>
					</div>
					<div class="modal-body">
						<fieldset>
							<div class="form-group">
								<div id="file-uploader"></div>
								<input type=hidden id="uploadfile" name="uploadfile" value="">
								<div id="uploaded"></div>
							</div>
						</fieldset>
					</div>
					<div class="modal-footer">
						<input type="submit" class="btn btn-primary" value="Đồng ý" />
					</div>
				</form>
			</div>
		</div>
	</div>

	<div class="modal fade" id="attach_task" data-width="700">
		<div class="modal-dialog">
			<div class="modal-content">
				<form action="javascript:attach_task();" id="form_attach_task" class="form-horizontal">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						<h4 class="modal-title">Cập nhật công việc</h4>
					</div>
					<div class="modal-body">
						<fieldset>
							<div class="form-group">
								<label class="control-label col-lg-3">Người nhận:</label>
								<div class="col-lg-9">
									<select id="att_uid" class="form-control">
										<option value="0">Chọn người nhận</option>
										<?php foreach($_SESSION['room_data'] as $room):?>
										<optgroup label="<?php echo $room['room_name'];?>">
											<?php foreach($room['users'] as $user):?>
											<option value="<?php echo $user['user_id'];?>"><?php echo $user['user_fullname'];?></option>
											<?php endforeach;?>
										</optgroup>
										<?php endforeach;?>
									</select>
								</div>
							</div>
							<div class="form-group">
								<label class="control-label col-lg-3">Tên công việc:</label>
								<div class="col-lg-9">
									<select name="tid" id="att_tid" class="form-control">
										<option value="0">Vui lòng chọn người nhận công việc</option>
									</select>
								</div>
							</div>
						</fieldset>
					</div>
					<div class="modal-footer">
						<input type="hidden" name="mod" value="attach_task" />
						<input type="hidden" name="req" value="<?php echo $this->phieu['req_id'];?>" />
						<input type="submit" class="btn btn-primary" value="Đồng ý" />
					</div>
				</form>
			</div>
		</div>
	</div>

	<script type="text/javascript" src="resources/scripts/jquery.printElement.min.js"></script>
	<link href="resources/css/fileuploader.css" type="text/css" rel="stylesheet" />
	<script type="text/javascript" src="resources/scripts/fileuploader.js"></script>
	<script>
		var uploader = new qq.FileUploader({
			element: document.getElementById('file-uploader'),
			action: 'file.php?act=upload',
			onComplete: function(id, fileName, responseJSON){
				var kq = responseJSON['success'];
				  if(kq == true){
					  var a = $("#uploadfile").val();
					  $("#uploadfile").val(a+';'+responseJSON['filename']);
					 }
			},
			debug: false
		});

		function doPrint(){
			$(".notPrint").hide();
			$('#printable').printElement();
			$(".notPrint").show();
		}

		$("#att_uid").change(function(){
			$.ajax({
				type: "POST",
				url: "?func=request_ajax&ajax=true",
				data:{
					mod:'search_task',
					uid:$(this).val()
				},
				dataType: 'json',
				success: function(msg){
					var options = '';
					for (var i = 0; i < msg.length; i++) {
						options += '<option value="' + msg[i].tid + '">' + msg[i].taskname + '</option>';
					}
					$("#att_tid").html(options);
				}
			});
		});
	</script>
<?php if(isset($this->breadcrumb) && isset($this->title_header)):?>
	<script type="text/javascript">
		$(function() {
			$("#title-header").text('<?php echo $this->title_header;?>');
			$("#breadcrumb").html('<?php echo build_breadcrumb($this->breadcrumb);?>');
		});
	</script>
<?php endif;?>
<?php
	if($_REQUEST['ajax']!=true) include 'footer.tpl.php';
?>